package com.gcxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.gcxy.entity.Project;
import com.gcxy.entity.WeeklyProject;
import com.gcxy.mapper.WeeklyProjectMapper;
import com.gcxy.service.ProjectService;
import com.gcxy.service.WeeklyProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gcxy.utils.BeanCopyUtil;
import com.gcxy.vo.weekly.ProjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Huang先森
 * @since 2024-07-22
 */
@Service
public class WeeklyProjectServiceImpl extends ServiceImpl<WeeklyProjectMapper, WeeklyProject> implements WeeklyProjectService {

    @Autowired
    private ProjectService projectService;

    /**
     * 根据周报ID获取周报项目
     *
     * @param weeklyId
     * @return
     */
    @Override
    public List<WeeklyProject> getByWeeklyId(Integer weeklyId) {
        return list(new LambdaQueryWrapper<WeeklyProject>().eq(WeeklyProject::getWeeklyId, weeklyId));
    }

    /**
     * 根据周报ID获取周报项目
     *
     * @param weeklyIds
     * @return
     */
    @Override
    public List<WeeklyProject> getByWeeklyId(List<Integer> weeklyIds) {
        return list(new LambdaQueryWrapper<WeeklyProject>().in(WeeklyProject::getWeeklyId, weeklyIds));
    }

    /**
     * 根据周报ID获取项目
     *
     * @param weeklyId
     * @return
     */
    @Override
    public List<ProjectVO> getProjectByWeeklyId(Integer weeklyId) {
        List<WeeklyProject> weeklyProjectList = getByWeeklyId(weeklyId);
        if (CollectionUtils.isEmpty(weeklyProjectList)) {
            return new ArrayList<>();
        }
        return weeklyProjectList.stream().map(weeklyProject -> {
            Project project = projectService.getById(weeklyProject.getProjectId());
            return BeanCopyUtil.copy(project, ProjectVO.class);
        }).collect(Collectors.toList());
    }

    /**
     * 根据周报ID获取项目
     *
     * @param weeklyIds
     * @return
     */
    @Override
    public List<ProjectVO> getProjectByWeeklyId(List<Integer> weeklyIds) {
        List<WeeklyProject> weeklyProjectList = getByWeeklyId(weeklyIds);
        if (CollectionUtils.isEmpty(weeklyProjectList)) {
            return new ArrayList<>();
        }
        return weeklyProjectList.stream().map(weeklyProject -> {
            Project project = projectService.getById(weeklyProject.getProjectId());
            return BeanCopyUtil.copy(project, ProjectVO.class);
        }).distinct().collect(Collectors.toList());
    }

    /**
     * 根据周报ID删除周报项目
     *
     * @param weeklyId
     * @return
     */
    @Override
    public void removeByWeeklyId(Integer weeklyId) {
        remove(new LambdaQueryWrapper<WeeklyProject>().eq(WeeklyProject::getWeeklyId, weeklyId));
    }

    /**
     * 批量添加周报项目
     *
     * @param projectList
     * @param weeklyId
     */
    @Override
    public void addWeeklyProjectList(List<Project> projectList, Integer weeklyId) {
        if (CollectionUtils.isNotEmpty(projectList)) {
            projectList.forEach(project -> {
                WeeklyProject weeklyProject = new WeeklyProject();
                weeklyProject.setProjectId(project.getId());
                weeklyProject.setWeeklyId(weeklyId);
                save(weeklyProject);
            });
        }
    }
}
